﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;

namespace JKF.Substructure
{
    /// <summary>
    /// 读取远程的文件,配置信息
    /// </summary>
    public class RemoteConfig
    {
        public static XElement Root
        {
            get
            {
                var key = SysConfig.ElementValue("remoteCfg");
                if (Cache.Exist(key))
                {
                    return Cache.Get<XElement>(key);
                }
                else
                {
                    WebRequest request = WebRequest.Create(key);
                    WebResponse response = request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
                    string s = reader.ReadToEnd();
                    var root = XDocument.Parse(s).FirstNode as XElement;
                    Cache.Add(key, root, new TimeSpan(0, 0, 10, 10));
                    return root;
                }
            }
        }

        public static string ElementValue(string nodeName)
        {
            var root = Root;
            return root.Element(nodeName).Value.Trim();
        }

        public static string HisUrl(string region)
        {
            if (string.IsNullOrEmpty(region))
            {
                return "#";
            }
            var sql = "select HIS_MAINURL from SYS_REGION where REGION_KEY = :a";
            return DB.HR.ExecuteDataSetSql(sql, region).Tables[0].Rows[0][0].ToString();
        }
    }
}
